package com.unnamed.b.atv.sample.holder;

import com.unnamed.b.atv.model.TreeNode;
import com.unnamed.b.atv.sample.ResourceTable;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.app.Context;

/**
 * IconTreeItemHolder
 *
 * @author Administrator
 * @Date 2021/5/26
 */
public class IconTreeItemHolder extends TreeNode.BaseNodeViewHolder<IconTreeItemHolder.IconTreeItem> {
    private Image arrowView;

    public IconTreeItemHolder(Context context) {
        super(context);
    }

    @Override
    public Component createNodeView(final TreeNode node, IconTreeItem value) {
        Component component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_icon_node, null, false);
        Text tvValue = (Text) component.findComponentById(ResourceTable.Id_node_value);
        tvValue.setText(value.text);
        Image iconView = (Image) component.findComponentById(ResourceTable.Id_icon);
        iconView.setPixelMap(value.icon);
        arrowView = (Image) component.findComponentById(ResourceTable.Id_arrow_icon);
        component.findComponentById(ResourceTable.Id_btn_addFolder).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                TreeNode newFolder = new TreeNode(new IconTreeItem(ResourceTable.Media_folder_close, "New Folder"));
                getTreeView().addNode(node, newFolder);
            }
        });
        component.findComponentById(ResourceTable.Id_btn_delete).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                getTreeView().removeNode(node);
            }
        });
        if (node.getLevel() == 1) {
            component.findComponentById(ResourceTable.Id_btn_delete).setVisibility(Component.INVISIBLE);
        }
        return component;
    }

    @Override
    public void toggle(boolean active) {
        arrowView.setPixelMap(active ? ResourceTable.Media_arrow_down : ResourceTable.Media_arrow_right);
    }

    /**
     * 添加视图显示的内容
     */
    public static class IconTreeItem {
        private int icon;
        private String text;

        /**
         * 初始化参数
         *
         * @param icon 图片资源文件
         * @param text 内容
         */
        public IconTreeItem(int icon, String text) {
            this.icon = icon;
            this.text = text;
        }

        /**
         * 取值
         *
         * @return 图片资源文件
         */
        public int getIcon() {
            return icon;
        }

        /**
         * 取值
         *
         * @return 内容
         */
        public String getText() {
            return text;
        }
    }
}
